﻿package com.foxhis.webstart.gui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.io.File;
import java.security.AccessController;
import java.security.PrivilegedAction;

import javax.swing.JApplet;
import javax.swing.JOptionPane;

import netscape.javascript.JSObject;

import com.foxhis.webstart.download.DownloadPanel;

/**
 * 程序检测入口
 * 
 * @author nestor
 * 
 * 2009-7-22 上午11:47:45
 */
public class StartMain extends JApplet {

	// private static final long serialVersionUID = 1L;

	// public static final String version = "Vesion:1.5.0";

	public String url = "";

	public String ip = "";

	public String mainName = ""; //下载主程序名
	public String serverName = ""; //系统名
	public String modelNum = "0";
//	public String isCheckVersion;
	
//	public String type;
	 
	//public String xmluri;
	
//	public String hotelids;
	public String serverurl;
	public String serverxml;
	public String versionxml;
	public String modelxml;
	public String hotelxml;
	
	public String username="";
	public String password="";
	public String hotelid="";
	public String language="";
	
	public String hotelurl="";
	
	public boolean isSinfornia=false;
	
	
	private DownloadPanel downloadPanel;
	public boolean islogin=false;
	private StartMain applet;
	private int count=0;
	public StartMain() {
		
	}
	

	public void init() {
		long inittime=System.currentTimeMillis();
		this.applet=this;
		System.out.println("hasinit-------------------");
		setBackground(new Color(194, 204, 198));
		setSize(500, 30);
		setLayout(new BorderLayout());
		if(downloadPanel==null){
			downloadPanel=new DownloadPanel(applet);
		}
		add(downloadPanel, BorderLayout.CENTER);
		setVisible(true);
//		initapplet();
		System.out.println("StartInitTime-----"+(System.currentTimeMillis()-inittime));
	}

	public String actionJS(String name) {
		String s = null;
		try {
			JSObject win = JSObject.getWindow(this);
			Object result=win.eval(name);
			s = result+"";
		} catch (Exception e) {
			e.printStackTrace();
		}
		return s;
	}
	
	private boolean initapplet(){
		long inittime=System.currentTimeMillis();
		String tempwebm=actionJS("getServerXml()");
		String htemp=actionJS("getHotelXml()");
		if(!(htemp==null||htemp.equals("null")||htemp.isEmpty())){
			String[] thotelid=htemp.split(",");
			hotelid=thotelid[0];
			if(thotelid.length>1){
				hotelurl=thotelid[1];
			}
		}
		String[] message=tempwebm.split("\",\"");
		serverurl=message[6];//actionJS("getServerURL()");
		serverxml=message[0];//actionJS("getServerXml()");
		System.out.println(serverxml);
		
		versionxml=message[1];//actionJS("getVersionXml()");
		String tmodelxml=message[2];//actionJS("getModelXml()");
		modelxml=tmodelxml;
		hotelxml=message[3];//actionJS("getHotelXml()");
		if(hotelxml!=null){
			if(hotelxml.trim().endsWith(",")){
				hotelxml=hotelxml.trim()+",";
				hotelxml=hotelxml.replace(",,", "");
			}
			if(!(hotelid==null||hotelid.isEmpty())){
				if(!hotelxml.contains(hotelid+"/")){
					JOptionPane.showMessageDialog(null, "酒店不存在！","Download...", JOptionPane.ERROR_MESSAGE);
					return false;
				}

			}
		}
		
		System.out.println("HotelList:"+hotelxml);
		url = message[4];//actionJS("getURL()");// "http://10.3.8.103:8099";//
		ip = message[5];//actionJS("getIP()");// "10.3.8.103";//"10.3.8.103";//
		if(modelxml!=null){
			String[] modlesplit=modelxml.split(",");
			modelxml="";
			for(String temp:modlesplit){
				if(!temp.isEmpty()&&temp.contains("/")){
					modelxml+=temp.substring(0, temp.indexOf("/"))+",";
				}
			}
			if(!modelxml.isEmpty()){
				modelxml+=",";
				modelxml=modelxml.replace(",,", "");
			}
		}
		System.out.println("ModelList:"+modelxml);
		serverName="pms";
		mainName="pms";
		modelNum =message[7];// actionJS("getServerName()");
		if(!modelNum.equals("LOADSYSTEM")){
			if(!modelNum.equals(serverName)){
				islogin=true;
			}
//			if(!modelNum.matches("[0-9]*")){
//				modelNum=tmodelxml.substring(0,tmodelxml.indexOf("/"));
//			}
		}
		else{
			islogin=false;
		}

		System.out.println("InitTime-----"+(System.currentTimeMillis()-inittime));
		return true;
	}
	
	@Override
	public void start() {
		System.out.println("start");
		downloadFile();
		super.start();
	}
	
	@Override
	public void stop() {
		System.out.println("stop!!!");
		super.stop();
		count=0;
	}
	
	@Override
	public void destroy() {
		System.out.println("destroy!!!");
		super.destroy();
	}
	
	public void downloadFile(){
		System.out.println("downloadfile!!!!");
			if(!initapplet()){
				applet.downloadPanel.closeDownload();
				return;
			}
			long inittime=System.currentTimeMillis();
			setEnabled(true);
			if(islogin){
				isSinfornia=true;
				actionJS("setMainModel('"+modelNum+"')");
			}else if(!modelNum.equals("LOADSYSTEM")){
				actionJS("setMainModel('"+modelNum+"')");
			}
			System.out.println("setMainTime-----"+(System.currentTimeMillis()-inittime));
			inittime=System.currentTimeMillis();
			AccessController.doPrivileged(new PrivilegedAction<String>() {
				@Override
				public String run() {
					if(!(hotelurl==null||hotelurl.isEmpty())){
						System.out.println("Hotelid-------------------"+hotelid);
						System.out.println("HotelURL-------------------"+hotelurl);
						File file=new File(hotelurl);
						if(!file.exists()){
							System.out.println("共享路径（"+hotelurl+"）不存在！");
							hotelurl="";
						}
						if(!hotelurl.endsWith("/")&&!hotelurl.endsWith("\\")){
							hotelurl+="\\";
						}
					}
					
					
					long inittime=System.currentTimeMillis();
					applet.downloadPanel.downloadFile();
					System.out.println("DownloadFileTime-----"+(System.currentTimeMillis()-inittime));
					return null;
				}
			});
			System.out.println("StartTime-----"+(System.currentTimeMillis()-inittime));
	}
	
	private String getSpliValue(String[] split,String defaultvalue){
		if(split.length>1){
			return split[1];
		}
		else{
			return "";
		}
	}
	
	public String gettest(){
		return "test";
	}
}
